package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.utils.QiniuUtils;
import com.atguigu.constant.RedisConstant;
import com.atguigu.dao.SetmealDao;
import com.atguigu.pojo.Setmeal;
import com.atguigu.service.SetmealService;
import com.atguigu.vo.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: meinian_parent
 * @author: Mr.Zhuo
 * @create: 2022-02-14 21:19
 **/
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealDao setmealDao;

    @Autowired
    private JedisPool jedisPool;


    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);

        Page<Setmeal> page = setmealDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public List<Integer> findTravelgroupIdBySetmealId(Integer id) {
        return setmealDao.findTravelgroupIdBySetmealId(id);
    }

    @Override
    public void delete(Integer id) {
        // 首先我们需要查询当前套餐是否有外键关联
        Integer count = setmealDao.selectConutById(id);
        if (count > 0) {
            throw new RuntimeException("请解除对应组团游的关系再来删除");
        }

        // 删除在七牛云上的数据
        Setmeal setmeal = setmealDao.selectById(id);
        String img = setmeal.getImg();
        QiniuUtils.deleteFileFromQiniu(img);

        // 删除redis中的数据
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, setmeal.getImg());

        // 然后删除对应t_setmeal中数据
        setmealDao.deleteById(id);
    }

    @Override
    public Setmeal findSetmealById(Integer id) {
        return setmealDao.findSetmealById(id);
    }

    @Override
    public List<Map<String, Object>> findSetmealNamesAndSetmealCount() {
        return setmealDao.findSetmealNamesAndSetmealCount();
    }



    @Override
    public void update(Integer[] travelgroupIds, Setmeal setmeal) {
        // 先找到id值 删除七牛云中的图片
        String img = setmealDao.selectImgById(setmeal.getId());
        QiniuUtils.deleteFileFromQiniu(img);
        // 删除redis中的值
        // 删除redis中的数据
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, setmeal.getImg());
        setmealDao.update(setmeal);

        // 然后暴力删除所有的关联属性
        setmealDao.deleteAllTravelgroupIdBySetmealId(setmeal.getId());

        // 然后重新保存相关关联的属性
        setSetmealAndTravelGroup(setmeal.getId(), travelgroupIds);
    }

    @Override
    public void add(Integer[] travelgroupIds, Setmeal setmeal) {
        // 分步添加
        setmealDao.add(setmeal);

        Integer setmealId = setmeal.getId();

        setSetmealAndTravelGroup(setmealId, travelgroupIds);

        savePic2Redis(setmeal.getImg());
    }

    //将图片名称保存到Redis
    private void savePic2Redis(String pic) {
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, pic);
    }


    private void setSetmealAndTravelGroup(Integer setmealId, Integer[] travelgroupIds) {
        for (Integer travelgroupId : travelgroupIds) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("setmealId", setmealId);
            map.put("travelgroupId", travelgroupId);

            // 调用方法进行储存
            setmealDao.setSetmealAndTravelGroup(map);
        }
    }


}
